from abc import ABC, abstractmethod
from typing import Optional

from pydantic import BaseModel, Field

from app.llm import LLM
from app.schema import Memory, AgentState


class BaseAgent(BaseModel, ABC):
    """用于管理智能体状态和执行的抽象基类。 这个类似Java中的接口，定义了一些规范
        它为状态转换、内存管理以及基于步骤的执行循环提供了基础功能。子类必须实现 step 方法。.
    """
    # agent 的名称 比较重要全局唯一
    name: str = Field(..., description="Unique name of the agent")
    description: Optional[str] = Field(None, description="Optional agent description")
    # Prompts
    system_prompt: Optional[str] = Field(
        None, description="System-level instruction prompt"
    )
    next_step_prompt: Optional[str] = Field(
        None, description="Prompt for determining next action"
    )

    # Execution control
    max_steps: int = Field(default=10, description="Maximum steps before termination")
    current_step: int = Field(default=0, description="Current step in execution")

    duplicate_threshold: int = 2

    # Dependencies
    # 大模型属性
    llm: LLM = Field(default_factory=LLM, description="Language model instance")

    # 记忆模块
    memory: Memory = Field(default_factory=Memory, description="Agent's memory store")
    # 状态
    state: AgentState = Field(
        default=AgentState.IDLE, description="Current agent state"
    )

    class Config:
        arbitrary_types_allowed = True
        extra = "allow"  # Allow extra fields for flexibility in subclasses

    @abstractmethod
    async def step(self) -> str:
        """在智能体的工作流程中执行单个步骤 子类必须实现
        """
